package com.yonyou.crm.sys.modules.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.crm.sys.modules.rmitf.IModuleRmService;

@RestController
@RequestMapping(value = "/sys")
public class ModuleCtrl {
	
	@Autowired
	private IModuleRmService service;

	/**
	 * 模块列表
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/modules", method = RequestMethod.GET)
	public Object getModuleList(HttpServletRequest request, HttpServletResponse response){
		List<ModuleVO> moduleList = service.getModuleList();
		List<Object> mList = new ArrayList<Object>();
		
		// 去掉明细对象
		Iterator<ModuleVO> iterator = moduleList.iterator();
		while (iterator.hasNext()) {
			ModuleVO moduleVO = iterator.next();
			if (moduleVO.getMasterObjId() != 0 || moduleVO.getIsExtensible() == 0) {
				iterator.remove();
			} else {
				Map<String, Object> mMap = new HashMap<String, Object>();
				mMap.put("id", moduleVO.getId());
				mMap.put("name", moduleVO.getName());
				mMap.put("apiName",moduleVO.getApiName());
				mList.add(mMap);
			}
		}
		JSONObject result = new JSONObject();
		result.put("data", mList);
		return ResponseUtil.getObject(result);
	}
	
	@RequestMapping(value = "/modules/{id}", method = RequestMethod.GET)
	public Object getModule(@PathVariable("id") Long id, HttpServletResponse response){
		ModuleVO moduleVO = service.getModule(id);
		JSONObject result = new JSONObject();
		result.put("data", moduleVO);
		return ResponseUtil.getObject(result);
	}
	
}
